define(["jquery", "easy-admin"], function ($, ea) {
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return decodeURI(pair[1]);  //处理中文显示异常
            }
        }
    }
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'mall.order/index',
        status_url: 'statusIndex',
        virtual_url: 'mall.order/virtualOrder',
        add_url: 'mall.order/add',
        permit_refund_url: 'mall.order/permitRefund',
        confirm_url: 'mall.order/confirmOrder',
        refund_url: 'mall.order/refund',
        send_url: 'mall.order/send',
        detail_url: 'mall.order/detail',
        export_url: 'mall.order/export',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh'],
                cols: [[
                    {field: 'order_no', title: '订单号'},
                    {field: 'collage.collage_no', title: '拼团号'},
                    {field: 'user.name', title: '用户名'},
                    {field: 'goods_name', title: '商品名'},
                    {field: 'express_no', title: '物流单号', hide: true},
                    {field: 'pay_price', title: '实际付款金额', templet: ea.table.price, search: false},
                    {field: 'pay_balance', title: '余额支付金额', templet: ea.table.price, search: false},
                    {field: 'order_price', title: '支付总额', templet: ea.table.price, search: false},
                    {field: 'failure_subsidy', title: '失败补贴金额', templet: ea.table.price, search: false},
                    {field: 'success_reward', title: '拼团成功奖励', templet: ea.table.price, search: false},
                    {
                        field: 'order_status',
                        title: '订单状态',
                        selectList: {
                            // 0: '未支付',
                            10: '拼团中',
                            30: '拼团成功',
                            40: '拼团失败',
                            50: '已发货',
                            60: '已完成',
                            70: '退款'
                        },
                        // templet: ea.table.switch
                    },
                    {field: 'create_time', title: '创建时间', search: 'false'},
                    {
                        field: 'ea_mall_order-create_time_left',
                        minWidth: 80,
                        title: '创建开始时间',
                        search: 'rangeLeft',
                        hide: true,
                        searchOp: 'rangeLeft'
                    },
                    {
                        field: 'ea_mall_order-create_time_right',
                        minWidth: 80,
                        title: '创建结束时间',
                        search: 'rangeRight',
                        hide: true,
                        searchOp: 'rangeRight'
                    },
                    {
                        width: 250, title: '操作', templet: function (row) {
                            var operat = [ // 操作按钮
                                [
                                    {
                                        text: '订单详情',
                                        url: init.detail_url,
                                        method: 'open',
                                        field: 'id',
                                        auth: 'detail',
                                        class: 'layui-btn layui-btn-xs layui-btn-success',
                                        extend: 'data-full="true"',
                                    }
                                ]
                            ]
                            var order_status = row.order_status
                            var refund_status = row.refund_status
                            if (refund_status === 10) {
                                operat.push(
                                    [
                                        {
                                            class: 'layui-btn layui-btn layui-btn-xs',
                                            method: 'get',
                                            field: 'id',
                                            icon: '',
                                            text: '退款',
                                            title: '确定退款？',
                                            auth: 'refund',
                                            url: init.refund_url,
                                            extend: ""
                                        }
                                    ]
                                )
                            }
                            if (order_status === 50) {
                                operat.push(
                                    [
                                        {
                                            class: 'layui-btn layui-btn layui-btn-xs',
                                            method: 'get',
                                            field: 'id',
                                            icon: '',
                                            text: '确认收货',
                                            title: '确认收货？',
                                            auth: 'confirm',
                                            url: init.confirm_url,
                                            extend: ""
                                        }
                                    ]
                                )
                            }
                            if (order_status === 30) {
                                operat.push(
                                    [
                                        {
                                            class: 'layui-btn layui-btn-success layui-btn-xs',
                                            method: 'open',
                                            field: 'id',
                                            icon: '',
                                            text: '发货',
                                            title: '发货信息',
                                            auth: 'send',
                                            url: init.send_url,
                                            extend: 'data-full="true"'
                                        }
                                    ]
                                )
                            }

                            return ea.table.tool(row, {
                                init, // 初始化的数据变量
                                operat: operat
                            }); // 其他状态下 没有操作按钮返回空字符串即可
                        }
                    },
                ]],
            });

            ea.listen();
        },
        statusIndex: function () {
            ea.table.render({
                init: init,
                url:init.status_url,
                where: {status: getQueryVariable('status')},
                toolbar: ['refresh'],
                cols: [[
                    {field: 'order_no', title: '订单号'},
                    {field: 'collage.collage_no', title: '拼团号'},
                    {field: 'user.name', title: '用户名'},
                    {field: 'goods_name', title: '商品名'},
                    {field: 'express_no', title: '物流单号', hide: true},
                    {field: 'pay_price', title: '实际付款金额', templet: ea.table.price, search: false},
                    {field: 'pay_balance', title: '余额支付金额', templet: ea.table.price, search: false},
                    {field: 'order_price', title: '支付总额', templet: ea.table.price, search: false},
                    {field: 'failure_subsidy', title: '失败补贴金额', templet: ea.table.price, search: false},
                    {field: 'success_reward', title: '拼团成功奖励', templet: ea.table.price, search: false},
                    {field: 'create_time', title: '创建时间', search: 'false'},
                    {
                        field: 'ea_mall_order-create_time_left',
                        minWidth: 80,
                        title: '创建开始时间',
                        search: 'rangeLeft',
                        hide: true,
                        searchOp: 'rangeLeft'
                    },
                    {
                        field: 'ea_mall_order-create_time_right',
                        minWidth: 80,
                        title: '创建结束时间',
                        search: 'rangeRight',
                        hide: true,
                        searchOp: 'rangeRight'
                    },
                    {
                        width: 250, title: '操作', templet: function (row) {
                            var operat = [ // 操作按钮
                                [
                                    {
                                        text: '订单详情',
                                        url: init.detail_url,
                                        method: 'open',
                                        field: 'id',
                                        auth: 'detail',
                                        class: 'layui-btn layui-btn-xs layui-btn-success',
                                        extend: 'data-full="true"',
                                    }
                                ]
                            ]
                            var order_status = row.order_status
                            var refund_status = row.refund_status
                            if ([10].includes(order_status) && refund_status === 0) {
                                operat.push(
                                    [
                                        {
                                            class: 'layui-btn layui-btn layui-btn-xs',
                                            method: 'get',
                                            field: 'id',
                                            icon: '',
                                            text: '允许退款',
                                            title: '确定允许退款？',
                                            auth: 'permit_refund',
                                            url: init.permit_refund_url,
                                            extend: ""
                                        }
                                    ]
                                )
                            }
                            if (refund_status === 10) {
                                operat.push(
                                    [
                                        {
                                            class: 'layui-btn layui-btn layui-btn-xs',
                                            method: 'get',
                                            field: 'id',
                                            icon: '',
                                            text: '退款',
                                            title: '确定退款？',
                                            auth: 'refund',
                                            url: init.refund_url,
                                            extend: ""
                                        }
                                    ]
                                )
                            }
                            if (order_status === 50) {
                                operat.push(
                                    [
                                        {
                                            class: 'layui-btn layui-btn layui-btn-xs',
                                            method: 'get',
                                            field: 'id',
                                            icon: '',
                                            text: '确认收货',
                                            title: '确认收货？',
                                            auth: 'confirm',
                                            url: init.confirm_url,
                                            extend: ""
                                        }
                                    ]
                                )
                            }
                            if (order_status === 30) {
                                operat.push(
                                    [
                                        {
                                            class: 'layui-btn layui-btn-success layui-btn-xs',
                                            method: 'open',
                                            field: 'id',
                                            icon: '',
                                            text: '发货',
                                            title: '发货信息',
                                            auth: 'edit',
                                            url: init.send_url,
                                            extend: ""
                                        }
                                    ]
                                )
                            }

                            return ea.table.tool(row, {
                                init, // 初始化的数据变量
                                operat: operat
                            }); // 其他状态下 没有操作按钮返回空字符串即可
                        }
                    },
                ]],
            });

            ea.listen();
        },
        send: function () {
            ea.listen();
        },

    };
    return Controller;
});